<?php
include 'include/functions.php';
include 'include/SurvivalPicks.php';
//set session save path
session_save_path($session_path);
// Initialize a session. 
session_start();
$count++;
$sessionId = session_id();
dbConnect();
//check to see if a Weeknum value was passed, if not, set WeekNum to Weeknum.
if (($Weeknum == NULL) || ($Weeknum == "")) {
	$Weeknum = getWeek();
}
// Set Deadline wekk for joining the survivor pool
$DeadlineWeek = 9;
//check to make sure $Weeknum > 1 AND $Weeknum < 21
$Weeknum = checkWeekBounds($Weeknum);

if ($EnableSurvivor)
{
	if ($userID)
		$InorOut = enableSurvivalPicks($userID, $DeadlineWeek);
	else
		$needToLogin = true;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//Dtd html 4.01 Transitional//EN"
"http://www.w3.org/tr/html4/loose.dtd">
<html><!-- InstanceBegin template="/Templates/MAIN.dwt.php" codeOutsideHTMLIsLocked="false" -->
  <head>
    <!-- InstanceBeginEditable name="doctitle" -->
    <title>JoshHughes.com NFL Football Pool</title>
    <!-- InstanceEndEditable -->
	<link rel="stylesheet" href="css/football.css" type="text/css">
	<script src="http://jquery.com/src/latest/"></script>
	<script type="text/javascript" src="scriptaculous-js-1.7.0/lib/prototype.js"></script>
    <script type="text/javascript" src="scriptaculous-js-1.7.0/src/scriptaculous.js"></script>
    <script type="text/javascript" src="scriptaculous-js-1.7.0/src/effects.js"></script>
    <script type="text/javascript" src="scriptaculous-js-1.7.0/src/dragdrop.js"></script>
    <script type="text/javascript" src="scriptaculous-js-1.7.0/src/controls.js"></script>
	<!-- <script src="scripts/ajax.js" type="text/javascript"></script> -->
	<script type="text/javascript" language="javascript">
	// Dynamic Iframe loader
	function loadIframe(theFrame, theURL) {
		document.getElementById(theFrame).src=theURL;
	}
	
	// resizes Iframe according to content
	function resizeMe(obj){ 
	docHeight = mainContent.document.body.scrollHeight
	 obj.style.height = docHeight + 'px'
	 } 
	 
   var http_request = false;
   function makeRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_request.onreadystatechange = alertContents;
      http_request.open('GET', url + parameters, true);
      http_request.send(null);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('myspan').innerHTML = result;            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   function get(obj) {
      var getstr = "?";
      for (i=0; i<obj.childNodes.length; i++) {
         if (obj.childNodes[i].tagName == "INPUT") {
            if (obj.childNodes[i].type == "text") {
               getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
            }
            if (obj.childNodes[i].type == "checkbox") {
               if (obj.childNodes[i].checked) {
                  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
               } else {
                  getstr += obj.childNodes[i].name + "=&";
               }
            }
            if (obj.childNodes[i].type == "radio") {
               if (obj.childNodes[i].checked) {
                  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
               }
            }
         }   
         if (obj.childNodes[i].tagName == "SELECT") {
            var sel = obj.childNodes[i];
            getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
         }
         
      }
      makeRequest('submitpicks2.php', getstr);
   }
   
   function goSet(obj, val){
		obj.value = val;
	 } 
	</script>
    <!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
  </head>
<body>
<div id="pagewidth" >
	<div id="header" >
	
		<?PHP include_once("layout/header.html"); ?>
		
	</div>
			<div id="wrapper" class="clearfix" > 
			<div id="maincol" >

			<!-- InstanceBeginEditable name="Contents" -->
			
			<table width="550" cellpadding="5" cellspacing="0">
				<tr>
					<td align="center" height="1"><h3>Survivor Pool</h3></td>
				</tr>
				<tr>
					<td height="1">
					Also known as Survivor, Eliminator, Loser, and Knockout Pools.. The aim is simple: pick one straight-up winner each week. You can't use the same team twice. And if your selections get beat, well, you're out.
					</td>
				</tr>
			</table>
			<h3 style="color:#172693; ">Survival Standings Example</h3>
			<table cellspacing="0" cellpadding="3" border="1" style="border-collapse: collapse">
				<tr bgcolor="#FFFFFF">
					<TH width="100" align="left" valign="bottom" rowspan="2" style="border-left:  1px solid #fff; border-top: 1px solid #fff;">UserName</TH><TH colspan="8" align="center" style=" border-top: 1px solid #fff; border-left:  1px solid #fff; border-right: 1px solid #fff;">Week</TH>
				</tr>
				<tr bgcolor="#FFFFFF">
					<td width="40" align="center"><a href="#1" class="permLink">1</a></td>
					<td width="40" align="center"><a href="#2" class="permLink">2</a></td>
					<td width="40" align="center"><a href="#3" class="permLink">3</a></td>
					<td width="40" align="center"><a href="#4" class="permLink">4</a></td>
					<td width="40" align="center"><a href="#5" class="permLink">5</a></td>
					<td width="40" align="center"><a href="#6" class="permLink">6</a></td>
					<td width="40" align="center"><a href="#7" class="permLink">7</a></td>
					<td width="40" align="center"><a href="#8" class="permLink">8</a></td>
				</tr>
				<tr>
					<td><a href="#" class="permLink">Bob</a></td>
					<td align="center"><img src="../images/small/Broncos.gif" title="Broncos"/></td>
					<td align="center"><img src="../images/small/Redskins.gif" title="Redskins"/></td>
					<td align="center"><img src="../images/small/Browns.gif" title="Browns"/></td>
					<td align="center"><img src="../images/small/Giants.gif" title="Giants"/></td>
					<td align="center"><img src="../images/small/Seahawks.gif" title="Seahawks"/></td>
					<td align="center"><img src="../images/small/Raiders.gif" title="Raiders"/></td>
					<td align="center"><img src="../images/small/Colts.gif" title="Colts"/></td>
					<td align="center"><img src="../images/small/Patriots.gif" title="Patriots"/></td>
				</tr>
				<tr>
					<td><a href="#" class="permLink">Fred</a></td>
					<td align="center"><img src="../images/small/Packers.gif" title="Packers"/></td>
					<td align="center"><img src="../images/small/Colts.gif" title="Colts"/></td>
					<td align="center"><img src="../images/small/Bengals.gif" title="Bengals"/></td>
					<td align="center"><img src="../images/small/Jets.gif" title="Jets"/></td>
					<td align="center"><img src="../images/small/Raiders.gif" title="Raiders"/></td>
					<td align="center"><img src="../images/small/Patriots.gif" title="Patriots"/></td>
					<td align="center"><img src="../images/small/Browns.gif" title="Browns"/></td>
					<td align="center"><img src="../images/red-x.gif" title="OUT"/></td>
				</tr>
				<tr>
					<td><a href="#" class="permLink">Jason</a></td>
					<td align="center"><img src="../images/small/Broncos.gif" title="Broncos"/></td>
					<td align="center"><img src="../images/small/Steelers.gif" title="Steelers"/></td>
					<td align="center"><img src="../images/small/Patriots.gif" title="Patriots"/></td>
					<td align="center"><img src="../images/small/Colts.gif" title="Colts"/></td>
					<td align="center"><img src="../images/small/Dolphins.gif" title="Dolphins"/></td>
					<td align="center"><img src="../images/red-x.gif" title="OUT"/></td>
					<td align="center"><img src="../images/red-x.gif" title="OUT"/></td>
					<td align="center"><img src="../images/red-x.gif" title="OUT"/></td>
				</tr>
				<tr>
					<td><a href="#" class="permLink">George</a></td>
					<td align="center"><img src="../images/small/Steelers.gif" title="Steelers"/></td>
					<td align="center"><img src="../images/small/Broncos.gif" title="Broncos"/></td>
					<td align="center"><img src="../images/small/Dolphins.gif" title="Dolphins"/></td>
					<td align="center"><img src="../images/red-x.gif" title="OUT"/></td>
					<td align="center"><img src="../images/red-x.gif" title="OUT"/></td>
					<td align="center"><img src="../images/red-x.gif" title="OUT"/></td>
					<td align="center"><img src="../images/red-x.gif" title="OUT"/></td>
					<td align="center"><img src="../images/red-x.gif" title="OUT"/></td>
				</tr>
				<tr bgcolor="#FFFFFF">
					<th width="100" align="left" style="border-top: 3px solid #cb0000;">Eliminated:</th>
					<td align="center" style="border-top: 3px solid #cb0000;">0</td>
					<td align="center" style="border-top: 3px solid #cb0000;">0</td>
					<th align="center" style="border-top: 3px solid #cb0000;color: #cb0000">1</th>
					<td align="center" style="border-top: 3px solid #cb0000;">0</td>
					<th align="center" style="border-top: 3px solid #cb0000;color: #cb0000">1</th>
					<td align="center" style="border-top: 3px solid #cb0000;">0</td>
					<th align="center" style="border-top: 3px solid #cb0000;color: #cb0000">1</th>
					<td align="center" style="border-top: 3px solid #cb0000;">0</td>
				</tr>
				<tr bgcolor="#FFFFFF">
					<th width="100" align="left">Remaining:</th>
					<th>4</th>
					<th>4</th>
					<th>3</th>
					<th>3</th>
					<th>2</th>
					<th>2</th>
					<th>1</th>
					<th>1</th>
				</tr>
			</table>
			<br/>
			<hr color="#172693" size="1" width="550" align="left"/>
			<table width="550" cellpadding="5" cellspacing="0" border="0">
				<tr>
					<td colspan="2">Once the surivor pool begins you cannot join anymore.  
					This wouldn't be fair to the players who survived the previous weeks, and even less fair to the players who were eliminated in the previous weeks.  
					If you would like to play in the survivor pool, please enter before the first game of <b>week <?=$DeadlineWeek;?></b> begins.
					<?
					if ($myrow = mysql_fetch_array(mysql_query("SELECT GameDate, GameTime FROM NFLSchedule WHERE Week=$DeadlineWeek ORDER BY GameDate, GameTime LIMIT 0,1")))
						echo "(".date('l F jS, Y',strtotime($myrow["GameDate"])).", ".date('g:i A',strtotime($myrow["GameTime"]))." EST)";
					?>					
					</td>
				</tr>
				<tr><td colspan="2"><hr color="#172693" size="1" width="550" align="left"/></td></tr>
				<tr>
				<?
				if ($needToLogin)
				{
					echo "<td align=\"center\" height=\"1\" colspan=\"2\">You need to be logged in to enroll in the survivor pool.  (We need to know who you are first so we can add you name to the list.)</td>";
				}
				elseif ($InorOut != "")
				{
					if ($InorOut == "Expired")
						echo "<td align=\"left\" height=\"1\" colspan=\"2\">The entry deadline has passed for entering the survivor pool.  Sorry!</td>";
					else
						echo "<td align=\"left\" height=\"1\" colspan=\"2\">You have successfully entered the survivor pool.  You can make your survival picks on the <a href=\"picks.php\" class=\"permLink\">picks page</a>.</td>";
				}
				else
				{
					if (IsSurviving($userID))
					{
						echo "<td align=\"left\" height=\"1\" colspan=\"2\">";
						DisplayPicked($userID);
						echo "<br/>
						<form method=\"POST\" name=\"PicksForm\" action=\"submitpicks.php\">";
						DisplayAvailable($userID,$Weeknum);
						echo "<br/> Week $Weeknum survivor pick: ";
						SurvivorPickDropDown($userID,$Weeknum);
						echo "
						<input type=\"hidden\" name=\"UserID\" value=\"$userID\">
						<input type=\"hidden\" name=\"Week\" value=\"$Weeknum\">
						<input type=\"reset\" value=\"Reset Form\"> | <input type=\"Submit\" name=\"Submit\" value=\"Update Picks\">
						</form></td>";
					}
					else
					{
						mysql_query("SELECT PickEmailed FROM NFLSchedule WHERE Week=$DeadlineWeek AND PickEmailed=1");
						if(mysql_affected_rows() > 0)
							echo "<td align=\"left\" height=\"1\">The entry deadline has passed for entering the survivor pool.  Sorry!</td><td><form name=\"SurvivorForm\" action=\"$PHP_SELF\" method=\"POST\"><input type=\"Submit\" name=\"EnableSurvivor\" value=\"Enter Survivor Pool\" disabled/></form> </td>";
						else
							echo "<td align=\"center\" height=\"1\">Do you think you can \"survive\"?</td><td><form name=\"SurvivorForm\" action=\"$PHP_SELF\" method=\"POST\"><input type=\"Submit\" name=\"EnableSurvivor\" value=\"Enter Survivor Pool\" /></form> </td>";
					}
				}
				?>
				</tr>
			</table>
			<!-- InstanceEndEditable -->
			
			</div>

			<div id="leftcol" >
			
				<?PHP include_once("layout/left_column.php"); ?>
		
			</div>
			
		</div>
		
		<div id="footer" >
		
			<?PHP include_once("layout/footer.html"); ?>
			
		</div>
</div>
</body>
<!-- InstanceEnd --></html>